Ext.define('Fxt.id-card.Controller', {
    extend: 'Fxt.core.CrudController',
    stores: 'Fxt.id-card.Store',
    models: ['Fxt.id-card.Model'],
    views: ['Fxt.id-card.view.Viewport', 'Fxt.id-card.view.Grid', 'Fxt.id-card.view.QueryForm'],
    $doAddEditShare: true,
    $onInit: function() {
        var me = this;
        var queryForm = me.$getViewport().$getQueryForm();
        var grid = me.$getViewport().$getGrid();
        var module = me.$getModule();
        this.control({
            'button[action=create-share-task]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var userId = record.get("userId");
                        Fxt.requireWithLoading(module + '.view.CreateShareTaskForm', function() {
                            var form = Ext.create(module + '.view.CreateShareTaskForm');
                            var win = Ext.create('Fxt.core.view.Window', {
                                title: '创建邀请任务',
                                items: [form],
                                buttons: [{
                                    text: '确定',
                                    handler: function() {
                                        me.$createShareTask(win, form, userId);
                                    }
                                }]
                            });
                        });
                    });
                }
            },
            'button[action=open-share-link]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var shareLink = record.get("shareLink");
                        window.open('http://a.sina.lt/?' + shareLink);
                    });
                }
            }
        });
    },
    $createShareTask: function(win, form, userId) {
    	var me = this;
    	if (!form.isValid()) {
    	    return;
    	}
        form.getForm().submit({
    	    url: Fxt.path + '/chucai/create-share-task',
    	    waitMsg: '正在创建邀请任务',
    	    params: {
    	        userId: userId
    	    },
    	    success: function() {
    	        win.close();
                Fxt.success('创建成功');
                me.$doQuery();
    	    },
    	    failure: Fxt._defaultSubmitFailure
    	});
    }
});